package com.xinsoft.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.entity.dto.CusWarnRuleDTO;
import com.xinsoft.entity.dto.CusWarnRulePageParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.service.CusWarnRuleService;
import com.xinsoft.mapper.CusWarnRuleMapper;
import com.xinsoft.service.SysDictionaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author hsm
* @description 针对表【cus_warn_rule(预警规则)】的数据库操作Service实现
* @createDate 2023-07-07 13:46:17
*/
@Service
public class CusWarnRuleServiceImpl extends ServiceImpl<CusWarnRuleMapper, CusWarnRule>
    implements CusWarnRuleService {

    @Resource
    public SysDictionaryService sysDictionaryService;

    /**
     * 分页
     * @param param
     * @return
     */
    @Override
    public IPage<CusWarnRuleDTO> findPageListByParam(CusWarnRulePageParam param) {
        //分页
        Page<CusWarnRuleDTO> page = new Page<CusWarnRuleDTO>(param.getCurrentPage(), param.getPageSize());
        IPage<CusWarnRuleDTO> resultPage = this.baseMapper.findListByParam(page, param);
        return resultPage;
    }

    /**
     * 不分页
     * @param param
     * @return
     */
    @Override
    public List<CusWarnRuleDTO> findListByParam(CusWarnRulePageParam param) {
        List<CusWarnRuleDTO> list = this.baseMapper.findListByParam(param);
        return list;
    }

    /**
     * 添加
     * @param cusWarnRule
     * @return
     */
    @Override
    public CusWarnRule addCusWarnRule(CusWarnRule cusWarnRule) {
        save(cusWarnRule);
        return cusWarnRule;
    }

    /**
     * 编辑
     * @param cusWarnRule
     * @return
     */
    @Override
    public CusWarnRule editCusWarnRule(CusWarnRule cusWarnRule) {
        updateById(cusWarnRule);
        return cusWarnRule;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public void del(List<Integer> ids) {
        QueryWrapper<CusWarnRule> query = new QueryWrapper<>();
        //启用
        query.eq("do_able", 1);
        query.in("id", ids);
        List<CusWarnRule> rules = this.list(query);
        //判断是否已启用
        if (CollectionUtil.isNotEmpty(rules)) {
            throw new BDException("操作失败，" + rules.get(0).getRuleName() + "已启用无法删除！");
        }
        // 删除
        removeByIds(ids);

    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public CusWarnRuleDTO getCusWarnRule(Integer id) {
        CusWarnRule entity = getById(id);
        CusWarnRuleDTO cusWarnRuleDTO = new CusWarnRuleDTO();
        BeanUtils.copyProperties(entity,cusWarnRuleDTO);
        SysDictionary warnType = sysDictionaryService.getById(entity.getWarnType());
        SysDictionary warnLevel = sysDictionaryService.getById(entity.getWarnLevel());
        cusWarnRuleDTO.setWarnTypeName(warnType.getTypeName());
        cusWarnRuleDTO.setWarnLevelName(warnLevel.getTypeName());
        return cusWarnRuleDTO;
    }

}




